<?xml version='1.0'?>
<xsl:stylesheet xmlns:xsl='http://www.w3.org/1999/XSL/Transform' version='1.0'>

<xsl:template match="hierform">
	<xsl:variable name="nlevel" select="count(levels/level)"/>
	<xsl:variable name="thisname">
		<xsl:choose>
			<xsl:when test="@name"><xsl:value-of select="@name"/></xsl:when>
			<xsl:otherwise><xsl:text>x_form_hier</xsl:text></xsl:otherwise>
		</xsl:choose>
	</xsl:variable>
	<div class="hier-form">
	<form method="post">
		<xsl:attribute name="action"><xsl:value-of select="$x_baseurl"/><xsl:value-of select="action/@link"/>
		<xsl:choose>
			<xsl:when test="action/@type = 'form'">
				<xsl:text>&amp;form=</xsl:text>
				<xsl:value-of select="action/@name"/>
			</xsl:when>
			<xsl:when test="action/@type = 'service'">
				<xsl:text>&amp;service=</xsl:text>
				<xsl:value-of select="action/@name"/>
				<xsl:text>&amp;process=</xsl:text>
				<xsl:value-of select="action/@process"/>
			</xsl:when>
		</xsl:choose>
		</xsl:attribute>
		<xsl:attribute name="name"><xsl:value-of select="$thisname"/></xsl:attribute>
		<table class="hier-table" cellpadding="0" cellspacing="0" border="0">
			<!-- hidden field(s) to hold hierarchy (if not using drop-downs)
			<xsl:apply-templates select="levels/level" mode="field"/> -->
			<tr>
				<xsl:call-template name="hier-data">
					<xsl:with-param name="which">1</xsl:with-param>
				</xsl:call-template>
				<xsl:call-template name="hier-data">
					<xsl:with-param name="which">2</xsl:with-param>
				</xsl:call-template>
				<xsl:call-template name="hier-data">
					<xsl:with-param name="which">3</xsl:with-param>
				</xsl:call-template>
			</tr>
			<xsl:if test="@button">
				<tr><td class="grp-btn-cell">
						<xsl:attribute name="colspan">
							<xsl:value-of select="$nlevel"/>
						</xsl:attribute>
						<xsl:for-each select="button">
							<xsl:variable name="thisact">
								<xsl:choose>
									<xsl:when test="@form">form</xsl:when>
									<xsl:when test="@service">serv</xsl:when>
									<xsl:otherwise>link</xsl:otherwise>
								</xsl:choose>
							</xsl:variable>
							<xsl:call-template name="grpform-btn">
								<xsl:with-param name="btnname"><xsl:value-of select="."/></xsl:with-param>
								<xsl:with-param name="btnaction"><xsl:value-of select="$thisact"/></xsl:with-param>
								<xsl:with-param name="btnlink"><xsl:value-of select="@link"/></xsl:with-param>
								<xsl:with-param name="btnform"><xsl:value-of select="@form"/></xsl:with-param>
								<xsl:with-param name="btnservice"><xsl:value-of select="@service"/></xsl:with-param>
								<xsl:with-param name="btnprocess"><xsl:value-of select="@process"/></xsl:with-param>
							</xsl:call-template>
							<xsl:call-template name="nobrspace"/>
						</xsl:for-each>
						<xsl:call-template name="grpform-btns">
							<xsl:with-param name="which"><xsl:value-of select="@button"/></xsl:with-param>
						</xsl:call-template>
					</td>
				</tr>
			</xsl:if>
		</table>
	</form>
	</div>	
</xsl:template>

<xsl:template match="level" mode="field">
	<input type="Hidden">
		<xsl:attribute name="name"><xsl:value-of select="@name"/></xsl:attribute>
		<xsl:attribute name="value"><xsl:value-of select="@value"/></xsl:attribute>
	</input>
</xsl:template>

<xsl:template name="hier-data">
	<xsl:param name="which"/>
	<xsl:variable name="this_level" select="levels/level[number($which)]"/>
	<xsl:variable name="this_type" select="$this_level/@type"/>
	<xsl:variable name="this_id" select="$this_level/@value"/>
	<xsl:variable name="par_level" select="levels/level[number($which)-1]"/>
	<xsl:variable name="par_id" select="$par_level/@value"/>
	
	<xsl:choose>
		<xsl:when test="$this_id!=''">
			<td class="hier-cell">
				<table class="hier-lvl-table">
					<tr><td class="hier-lvl-cell-head"><span class="hier-lvl-text-head"><xsl:value-of select="$this_level/@name"/></span></td></tr>
					<tr>
						<td><select>
							<xsl:attribute name="name"><xsl:value-of select="$this_level/@id"/></xsl:attribute>
							<xsl:if test="$which=1 or $which=2">
								<xsl:attribute name="onchange">javascript:x_grp.Form(<xsl:value-of select="@name"/>, 'serv', '<xsl:value-of select="@link"/>', '', '<xsl:value-of select="@service"/>', '<xsl:value-of select="@process"/>')</xsl:attribute>
							</xsl:if>
								<option value="0"><xsl:text>Please select...</xsl:text>
									<xsl:if test="$this_id='0'">
										<xsl:attribute name="selected"></xsl:attribute>
									</xsl:if>
								</option>
								<xsl:choose>
									<xsl:when test="$which=1">
										<xsl:for-each select="Products/part[@type=$this_type]">
											<xsl:call-template name="hier-part">
												<xsl:with-param name="thisval"><xsl:value-of select="$this_id"/></xsl:with-param>
											</xsl:call-template>
										</xsl:for-each>
									</xsl:when>
									<xsl:when test="$which=2">
										<xsl:for-each select="Products/part[@id=$par_id]/part[@type=$this_type]">
											<xsl:call-template name="hier-part">
												<xsl:with-param name="thisval"><xsl:value-of select="$this_id"/></xsl:with-param>
											</xsl:call-template>
										</xsl:for-each>
									</xsl:when>
									<xsl:when test="$which=3">
										<xsl:for-each select="Products/part/part[@id=$par_id]/part[@type=$this_type]">
											<xsl:call-template name="hier-part">
												<xsl:with-param name="thisval"><xsl:value-of select="$this_id"/></xsl:with-param>
											</xsl:call-template>
										</xsl:for-each>
									</xsl:when>
									<xsl:otherwise>
									</xsl:otherwise>
								</xsl:choose>
							</select>
						</td>
					</tr>
				</table>
			</td>
		</xsl:when>
		<xsl:otherwise>
			<td><xsl:call-template name="nobrspace"/></td>
		</xsl:otherwise>
	</xsl:choose>
</xsl:template>

<xsl:template name="hier-part">
	<xsl:param name="thisval"/>
	<option>
		<xsl:attribute name="value"><xsl:value-of select="@id"/></xsl:attribute>
		<xsl:if test="@id=$thisval">
			<xsl:attribute name="selected"></xsl:attribute>
		</xsl:if>
		<xsl:value-of select="@name"/>
	</option>
</xsl:template>

</xsl:stylesheet>